package com.fc.test.service;

import cn.hutool.core.util.StrUtil;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.exception.DataException;
import com.fc.test.common.support.ConvertUtil;
import com.fc.test.mapper.auto.HhbFileMapper;
import com.fc.test.model.auto.HhbFile;
import com.fc.test.model.auto.HhbFileExample;
import com.fc.test.model.auto.TsysFile;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;
import com.fc.test.util.StringUtils;
import com.fc.test.vo.HhbFileVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 红黑榜文件管理 HhbFileService
 *
 * @author fuce_自动生成
 * @Title: HhbFileService.java 
 * @Package com.fc.test.service 
 * @email 1345625601@qq.com
 * @date 2020-11-28 17:43:14  
 **/
@Service
public class HhbFileService implements BaseService<HhbFile, HhbFileExample> {
    @Autowired
    private HhbFileMapper hhbFileMapper;
    @Autowired
    private SysFileService sysFileService;

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<HhbFile> list(Tablepar tablepar, String searchText) {
        HhbFileExample testExample = new HhbFileExample();
        testExample.setOrderByClause("id ASC");
        if (StrUtil.isNotEmpty(searchText)) {
            testExample.createCriteria().andStatusLike("%" + searchText + "%");
        }
        if (StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
            testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) + " " + tablepar.getIsAsc());
        }
        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
        List<HhbFile> list = hhbFileMapper.selectByExample(testExample);
        PageInfo<HhbFile> pageInfo = new PageInfo<HhbFile>(list);
        return pageInfo;
    }


    /**
     * 分页查询2
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<HhbFileVo> list2(Tablepar tablepar, String searchText) {

        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
        List<HhbFileVo> list = hhbFileMapper.list2(searchText);
        PageInfo<HhbFileVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(String ids) {

        List<String> lista = ConvertUtil.toListStrArray(ids);
        HhbFileExample example = new HhbFileExample();
        example.createCriteria().andIdIn(lista);
        sysFileService.deleteByPrimaryKey(ids);
        return hhbFileMapper.deleteByExample(example);


    }


    @Override
    public HhbFile selectByPrimaryKey(String id) {

        return hhbFileMapper.selectByPrimaryKey(id);

    }

    public HhbFileVo selectById(String id) {

        return hhbFileMapper.selectById(id);

    }


    @Transactional
    public int updateByPrimaryKeySelective2(HhbFile record) throws  DataException{
        if(record.getStatus() == 1){
            hhbFileMapper.updateStatusStop();
        }else { //状态0
            HhbFile s = hhbFileMapper.queryOther(record.getId());//查询其他是否有启用
            if(Objects.isNull(s)){ //全都是停用 提示 至少得有一个启用的
                throw new DataException("至少启用一个红黑榜");
            }
        }
        return hhbFileMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(HhbFile record) {

        return hhbFileMapper.updateByPrimaryKeySelective(record);
    }

    @Transactional
    public int update(HhbFile record,TsysFile tsysFile,String dataId) {
        sysFileService.updateByPrimaryKey(tsysFile,dataId);
        return hhbFileMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 添加
     */
    @Transactional
    public int insertSelective(HhbFile record, TsysFile file, String dataId) {



        String id = sysFileService.insertSelective(file, dataId);

        //添加雪花主键id
        record.setId(id);

        if(record.getStatus() == 1){//若启用，其他全都是停用，即只能启用一个
            hhbFileMapper.updateStatusStop();
        }
        return hhbFileMapper.insertSelective(record);
    }

    /**
     * 添加
     */
    @Override
    public int insertSelective(HhbFile record) {

        //添加雪花主键id
        record.setId(SnowflakeIdWorker.getUUID());

        return hhbFileMapper.insertSelective(record);
    }


    @Override
    public int updateByExampleSelective(HhbFile record, HhbFileExample example) {

        return hhbFileMapper.updateByExampleSelective(record, example);
    }


    @Override
    public int updateByExample(HhbFile record, HhbFileExample example) {

        return hhbFileMapper.updateByExample(record, example);
    }

    @Override
    public List<HhbFile> selectByExample(HhbFileExample example) {

        return hhbFileMapper.selectByExample(example);
    }


    @Override
    public long countByExample(HhbFileExample example) {

        return hhbFileMapper.countByExample(example);
    }


    @Override
    public int deleteByExample(HhbFileExample example) {

        return hhbFileMapper.deleteByExample(example);
    }

    /**
     * 检查name
     *
     * @param hhbFile
     * @return
     */
    public int checkNameUnique(String fileName) {
        List<String> list = hhbFileMapper.selectByFilename(fileName);
        return list.size();
    }


    public String queryStatusON() {
      return   hhbFileMapper.queryStatusON();
    }
}
